﻿
using LoadingPanelSample.HelperClasses;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Ribbon;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Navigation;
using mshtml;
using Forms = System.Windows.Forms;

namespace SeleniumRecorder
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : RibbonWindow, INotifyPropertyChanged
    {

        public MainWindow()
        {
            InitializeComponent();
            EnableTabs = false;
            Visible = Visibility.Hidden;
            LoadProgress.Opacity = 0;
            CollapseRib = false;
            UpdateFilters();
            DataContext = this;

            RibbonWin.SelectedIndex = 0;
        }

        public bool started = false;

       

        /// <summary>
        /// Comments for the customer
        /// </summary>
        private bool _collapse;


        /// <summary>
        /// Gets or sets the comments for the customer
        /// </summary>
        public bool CollapseRib
        {
            get
            {
                return _collapse;
            }
            set
            {
                _collapse = value;
                OnPropertyChanged("CollapseRib");
            }
        }

        /// <summary>
        /// Comments for the customer
        /// </summary>
        private bool _comments;


        /// <summary>
        /// Gets or sets the comments for the customer
        /// </summary>
        public bool EnableTabs
        {
            get
            {
                return _comments;
            }
            set
            {
                if (started)
                {
                    _comments = value;
                }
                else
                {
                    _comments = false;
                }
                OnPropertyChanged("EnableTabs");
                if (_comments && started)
                {
                    //// Trigger OnPropertyChanged for this property 
                    Visible = Visibility.Visible;
                    VisibleHome = Visibility.Collapsed;
                }
                else
                {
                    Visible = Visibility.Collapsed;
                    VisibleHome = Visibility.Visible;
                }
            }
        }
        private Visibility _visible;
        public Visibility Visible
        {
            get
            {
                return _visible;
            }
            set
            {
                _visible = value;
                //// Trigger OnPropertyChanged for this property
                OnPropertyChanged("Visible");
            }
        }

        private Visibility _VisibleHome;
        public Visibility VisibleHome
        {
            get
            {
                return _VisibleHome;
            }
            set
            {
                _VisibleHome = value;
                //// Trigger OnPropertyChanged for this property
                OnPropertyChanged("VisibleHome");
            }
        }



        /// <summary>
        /// Invoked when the property value on an element has been changed
        /// </summary>
        /// <param name="propertyName">The property name that changed</param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private WebBrowserHostUIHandler _wbHandler;

        /// <summary>
        /// Gets the change sub message command.
        /// </summary>
        public ICommand ChangeSubMessageCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    open();
                });
            }
        }

        public void open()
        {
            string address = URL.Text;

            if (String.IsNullOrEmpty(address)) return;
            if (address.Equals("about:blank")) return;
            if (!address.StartsWith("http://") &&
                !address.StartsWith("https://"))
            {
                address = "http://" + address;
            }
            try
            {

                ApplicationBrowser.AllowDrop = false;
                ApplicationBrowser.Navigate(new Uri(address));
                ApplicationBrowser.LoadCompleted += ApplicationBrowser_LoadCompleted;
                LoadProgress.Opacity = 1;
                LoadProgress.Value = 50;
            }
            catch (System.UriFormatException)
            {
                return;
            }

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

        }

        mshtml.DispHTMLDocument htmlDoc = null;
        public Dictionary<string, ControlDataStructure> proccontrol = new Dictionary<string, ControlDataStructure>();



        IHTMLElement selectElemt;

        void ApplicationBrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            LoadProgress.Value = 100;
            LoadProgress.Opacity = 0;

            htmlDoc = ApplicationBrowser.Document as mshtml.DispHTMLDocument;
            ((mshtml.HTMLDocumentEvents2_Event)htmlDoc).onmousedown += new mshtml.HTMLDocumentEvents2_onmousedownEventHandler(ApplicationBrowser_MouseDown);

        }

        void ApplicationBrowser_MouseDown(IHTMLEventObj e)
        {
            IHTMLElement elem = htmlDoc.activeElement;

            ContextMenu cm = this.FindResource("cmButton") as ContextMenu;

            cm.IsOpen = true;
        }



        #region Mouse Button


        void mainNode_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (ControlTree.SelectedItem != null)
            {
                ContextMenu cm = this.FindResource("TreeViewContext") as ContextMenu;

                cm.IsOpen = true;
            }
        }
        static TreeViewItem VisualUpwardSearch(DependencyObject source)
        {
            while (source != null && !(source is TreeViewItem))
                source = VisualTreeHelper.GetParent(source);

            return source as TreeViewItem;
        }
        void mainNode_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem treeViewItem = VisualUpwardSearch(e.OriginalSource as DependencyObject);

            if (treeViewItem != null)
            {
                treeViewItem.Focus();
                e.Handled = true;
            }

        }

        void mainNode_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //  highLightElement();
        }

        private void highLightElement()
        {
            try
            {
                if (selectElemt != null)
                {
                    dynamic s = selectElemt.getAttribute("style");
                    s.borderColor = "currentColor";
                    s.borderStyle = "none";
                    s.borderwidth = "medium";
                    //  selectElemt = webBrowser2.Document.GetElementById(node.Name.ToString());
                    selectElemt.setAttribute("style", s);
                }
            }
            catch
            {
            }
            TreeViewItem item = ControlTree.SelectedItem as TreeViewItem;

            if (!proccontrol.ContainsKey(item.Name))
            {
                return;
            }

            ControlDataStructure data = proccontrol[item.Name];


            if (data.ID != null)
            {
                selectElemt = htmlDoc.getElementById(data.ID);
            }
            else if (data.Name != null)
            {
                selectElemt = htmlDoc.getElementsByName(data.Name).item(name: data.Name);
            }

            if (selectElemt == null && data.x > 0 && data.y > 0)
            {
                selectElemt = htmlDoc.elementFromPoint(data.x, data.y);
            }

            if (selectElemt != null)
            {
                dynamic s = selectElemt.getAttribute("style");
                s.borderColor = "red";
                s.borderStyle = "solid";
                s.borderwidth = "5px";
                //  selectElemt = webBrowser2.Document.GetElementById(node.Name.ToString());
                selectElemt.setAttribute("style", s);
            }
        }



        #endregion



        private void FileMenu_DropDownOpened_1(object sender, System.EventArgs e)
        {
            EnableTabs = false;
        }

        private void FileMenu_DropDownClosed_1(object sender, System.EventArgs e)
        {
            EnableTabs = true;
        }

    }
}
